#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import os


HTML_Str = '''
<!DOCTYPE HTML>
<html>
<head>
    <title>HOME PAGE</title>
    <link rel="icon" href="resource/favicon.ico" type="image/x-icon"/>
     <link rel="shortcut icon" href="resource/favicon.ico" type="image/x-icon"/> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="text/JavaScript">
        function startSearch() {
            // https://www.google.com/search?q=
            // https://www.baidu.com/s?word=
            var str = document.getElementById("searchField").value;
            var url = "https://www.google.com/search?q=" + str;
            window.open(url);
        }
        function startBiyingSearch() {
            var str = document.getElementById("searchField").value;
            var url = "https://cn.bing.com/search?q=" + str;
            window.open(url);
        }
        function onEnterDown() {
            if(event.keyCode == 13) {   
                document.getElementById("searchButton").click();   
                return false;   
        } 
        }
    </script>
</head>
    <body id="body" >
          <script src="https://sf-cdn.coze.com/obj/unpkg-va/flow-platform/chat-app-sdk/0.1.0-beta.2/libs/oversea/index.js"></script>
      <script>
          new CozeWebSDK.WebChatClient({
            config: {
              bot_id: '7338383966245830662',
            },
            componentProps: {
              title: 'Coze',
            },
          });
      </script>
        <div style="background-color:#272822; 
        padding:15px;
        height:auto ;
        margin-bottom: 20px;">
            <h1 style="color:white;text-align:center">常用站点</h1>
        </div>

        <div style="
        margin:0 auto;
        margin-bottom:20px;
        width:740px;
        height:40px">
            <input id="searchField" type="text" onkeydown="onEnterDown();" placeholder="输入搜索内容" style="
            width:540px;
            height:100%;
            font-size:20px;">
            </input>

            <button id="searchButton" onclick="startSearch();" style="
            background-color:#169FE6;
            color:white;
            width:90px;
            height:100%;
            font-size:20px;">
            Google
            </button>

            <button id="searchBiyingButton" onclick="startBiyingSearch();" style="
            background-color:#169FE6;
            color:white;
            width:90px;
            height:100%;
            font-size:20px;">
            必应
            </button>
        </div>

        
        <div id = "conten_div" style="background-color:#F2F4F7; 
            height:fit-content;
            width:fit-content;
            min-width:760px;
            padding:20px;
            margin:0 auto;">
                {template}
        </div>
    </body>
</html>
'''
DIV_Str = '''
                <div style="width:fit-content;
                    text-align:left;
                    padding-top:3px;
                    padding-bottom:3px;">
                        {template}
                    </div>
'''
LI_Str = '''
                    <li style="display:inline-block;width:85px;
                    text-align:center;
                    margin-left:5px;
                    margin-right:5px;">
                        <a style="color:black;font-size:20px;" href="{url}" target="_blank">
                            {name}
                        </a>
                    </li>
'''
LINE_COUNT = 7


def shell_execute(commond):
    return os.system(commond)


def makeHtml():
    lists = []
    list = []
    lists.append(list)
    list.append({"name": "Acfun", "url": "https://www.acfun.cn/"})
    list.append({"name": "哔哩哔哩", "url": "https://www.bilibili.com/"})
    list.append({"name": "嘀哩嘀哩", "url": "http://www.dilidili.com/"})
    list.append({"name": "斗　鱼", "url": "https://www.douyu.com/directory/game/DOTA2"})
    list.append({"name": "爱 奇 艺", "url": "https://www.iqiyi.com/"})
    list.append({"name": "优  酷", "url": "https://www.youku.com/"})
    list.append({"name": "电 视 猫", "url": "http://www.tvmao.com/"})
    
    list = []
    lists.append(list)
    list.append({"name": "淘 宝 网", "url": "https://www.taobao.com/"})
    list.append({"name": "天　猫", "url": "https://www.tmall.com/"})
    list.append({"name": "京东商城", "url": "https://www.jd.com/"})

    list = []
    lists.append(list)
    list.append({"name": "网易音乐", "url": "https://music.163.com/#/user/home?id=1"})
    list.append({"name": "技术分享", "url": "https://cuijiahua.com/"})
    list.append({"name": "机核网", "url": "https://www.gcores.com/"})
    list.append({"name": "CSDN", "url": "https://www.csdn.net/"})
    list.append({"name": "开源中国", "url": "https://www.oschina.net/"})
    list.append({"name": "百度网盘", "url": "https://pan.baidu.com/disk/home/"})
    list.append({"name": "数据包", "url": "https://tushare.pro/"})

    list.append({"name": "GitHub", "url": "https://github.com/"})
    
    list.append({"name": "Coursera", "url": "https://www.coursera.org/"})
    list.append({"name": "VPNSO", "url": "https://vpnso.com/"})

    list = []
    lists.append(list)
    list.append({"name": "在线之家", "url": "http://www.1993s.top/"})
    list.append({"name": "樱花动漫", "url": "http://www.yinghuacd.com/"})
    list.append({"name": "GIMY", "url": "https://gimy.app/"})
    list.append({"name": "海外wetv", "url": "https://www.iflix.com/zh-cn/"})
    list.append({"name": "No视频", "url": "https://www.novipnoad.com/"})
    list.append({"name": "厂长视频", "url": "https://qianoo.cn/"})
    list.append({"name": "欧乐影院", "url": "https://www.olevod.com/"})
    list.append({"name": "茶杯狐", "url": "https://cupfox.app/"})
    list.append({"name": "LIBVIO", "url": "https://libvio.fun/type/1.html"})

    divLists = []
    for list in lists:
        liList = []
        for index in range(len(list)):
            row = int(index/LINE_COUNT)
            if len(liList) <= row:
                liList.append([])
            data = list[index]
            liStr = LI_Str.replace("{name}", data["name"]).replace("{url}", data["url"])
            liList[row].append(liStr)
        


        divList = []
        divLists.append(divList)
        for index in range(len(liList)):
            if len(divList) <= index:
                divList.append("")
            for index_ in range(len(liList[index])):
                divList[index] = divList[index] + liList[index][index_]
            divList[index] = DIV_Str.replace("{template}", divList[index])

    content = ""
    for divList in divLists:
        for index in range(len(divList)):
            content = content + divList[index]
        content = content + '''<div style="width:fit-content;
                    text-align:left;
                    padding-top:10px;
                    padding-bottom:10px;">
                     </div>'''
    html = HTML_Str.replace("{template}", content)

    project_root = os.path.dirname(os.path.realpath(__file__))
    path = os.path.join(project_root, "home.html")
    file = open(path, "w", encoding='utf8')
    file.write(html)
    file.close()


if __name__ == '__main__':
    '''获取本文件的路径'''
    project_root = os.path.dirname(os.path.realpath(__file__))
    # 切换到工作目录
    os.chdir(project_root)

    shell_execute("git pull")

    makeHtml()

    shell_execute("cp home.html public/index.html")
    shell_execute("git add --all")
    shell_execute('''git commit -m "update"''')
    shell_execute("git push")
    shell_execute("firebase deploy")
